//
//  YMTTabBarController.m
//  projectBase
//
//  Created by zhuangzhh on 14-12-3.
//  Copyright (c) 2014年 zhuangzhh. All rights reserved.
//

#import "YMTTabBarController.h"

@interface YMTTabBarController ()

@end

@implementation YMTTabBarController


-(void) tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item   //当点击相应得UITabBarItem时被调用
{
    switch (item.tag)
    {
        case HOMETAG:
        {
            [AppData shareInstance].indexOfSelectedTabItem = HOMETAG;
            break;
        }
        case CLASSTAG:
        {
            [AppData shareInstance].indexOfSelectedTabItem = CLASSTAG;
            if([AppData shareInstance].isLogin == NO)
            {
//                LoginController *loginVc = [[LoginController alloc] init];
//                loginVc.type = 1;
//                [[ViewManager shareInstance].NavigationController pushViewController:loginVc animated:YES];
            }
            break;
        }
        default:
        {
            [AppData shareInstance].indexOfSelectedTabItem = PersonTag;
//            NSString *isthird = [[NSUserDefaults standardUserDefaults]objectForKey:@"isthird"];
//            if (![isthird isEqualToString:@"1"])
//            {
//                LoginController *vc =[[LoginController alloc] init];
//                vc.type = 4;
//                [[ViewManager shareInstance].NavigationController pushViewController:vc animated:YES];
//            }
            if([AppData shareInstance].isLogin == NO)
            {
//                LoginController *loginVc = [[LoginController alloc] init];
//                loginVc.type = 4;
//                [[ViewManager shareInstance].NavigationController pushViewController:loginVc animated:YES];
            }
            break;
        }


    }
}



-(id)initYMTTabBarVC:(NSArray *)views
{
    self = [super init];
    if(self)
    {
        if(![views isKindOfClass:[NSNull class]]&& views != nil)
        {
            self.viewControllers = views;
            
            NSString *skin = [[NSUserDefaults standardUserDefaults] objectForKey:@"skin"];
//            if([skin isEqualToString:@"1"])
//            {
//                self.tabBar.tintColor = PINKCOlORSKIN;
//                [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:PINKCOlORSKIN, UITextAttributeTextColor,nil] forState:UIControlStateSelected];
//                [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:116.0/255 green:106.0/255 blue:103.0/255 alpha:1.0], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabbarAction:) name:Notify_TabBarOfIndex object:nil];
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabbarcolorAction:) name:Notify_TabBarOfColor object:nil];
//            }
//            else
//            {
//                self.tabBar.tintColor = REDCOLORSKIN;
//                [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:REDCOLORSKIN, UITextAttributeTextColor,nil] forState:UIControlStateSelected];
//                [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:116.0/255 green:106.0/255 blue:103.0/255 alpha:1.0], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
//                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabbarAction:) name:Notify_TabBarOfIndex object:nil];
//                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabbarcolorAction:) name:Notify_TabBarOfColor object:nil];
//            }

        }
    }
    return self;
}

-(void)tabbarcolorAction:(id)sender
{
//    self.tabBar.tintColor = MainColor;
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MainColor, UITextAttributeTextColor,nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:112.0/255 green:208.0/255 blue:73.0/255 alpha:1.0], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
}

-(void)setYMTTabBarItemImage:(NSArray *)normalImages highImage:(NSArray *)highImages
{
    if([normalImages count] != [highImages count])
        return;
    
    UITabBar *tabBar = self.tabBar;
    int barnum = [tabBar.items count];
    if(barnum > 0 && [normalImages count]>=barnum)
    {
        for(int index = 0;index < barnum ;index++)
        {
            UITabBarItem *tabBarItem = [tabBar.items objectAtIndex:index];
            if(index == 0)
                tabBarItem.tag = HOMETAG;
            else if(index == 1)
                tabBarItem.tag = CLASSTAG;
            else if(index == 2)
                tabBarItem.tag = PersonTag;
                       [tabBarItem setFinishedSelectedImage:[highImages objectAtIndex:index] withFinishedUnselectedImage:[normalImages objectAtIndex:index]];
        }
    }

}

-(void)setYMTTabBarItemTitle:(NSArray *)titles
{
    UITabBar *tabBar = self.tabBar;
    int barnum = [tabBar.items count];
    if(barnum > 0 && [titles count]>=barnum)
    {
        for(int index = 0;index < barnum ;index++)
        {
            UITabBarItem *tabBarItem = [tabBar.items objectAtIndex:index];
            tabBarItem.title = [titles objectAtIndex:index];

        }
    }
}

- (void)tabbarAction:(NSNotification *)notification {
    NSNumber *num = (NSNumber *)[notification object];
    [self setSelectedIndex:[num intValue]];

}

@end
